/* 11. 查看某个读者未还书籍信息 */
"use strict";

const CHECK = require("../myUtil/formatCheck");
const DATEUTIL = require("../myUtil/DateUtil");
const db = require("../../coSqlite3");
const { StrTime } = require("../myUtil/lib");
const HTM = require("../myUtil/lib").html;

exports.queryBookNotReturn = function* (req, res) {
  console.log("----------------------11. 查看某个读者未还书籍信息\n");
  // DATEUTIL.add30Days("2022-01-31");
  // console.log('2022-11-10'>'2022-11-01');
  // console.log(StrTime(new Date(), 'yyyy-mm-dd'));
  let rID = req.body.rID;

  let htm = HTM.tableBegin + HTM.tableEnd + "提交的参数有误";
  if (rID == "") {
    return HTM.head + htm + "证号不能为空" + HTM.end;
  } else if (rID.length > 8) {
    return HTM.head + htm + "证号长度不能超过8" + HTM.end;
  }

  let rows;
  try {

    let rIDExist = yield db.execSQL("SELECT count(rid) as count FROM reader WHERE rid = ?", [rID]);
    if(rIDExist[0].count == 0){
      htm = HTM.divBegin + 1 + HTM.divEnd;
      return HTM.head + htm + "该证号不存在" + HTM.end;
    }

    rows = yield db.execSQL("SELECT * FROM process WHERE returned = 0 and rid = ?", [rID]);
    htm = '';
    // let shouldReturnTime ;
    // console.log(rows[0].bname);
    for (let row of rows) {
      // shouldReturnTime = DATEUTIL.add30Days(row.create_time);
      // console.log(row.bname+": ssss");
      // console.log((StrTime(new Date(), 'yyyy-mm-dd') > shouldReturnTime) + " :SS");
      htm +=
        "<tr><td>" +
        row.bid +
        "</td><td>" +
        row.bname +
        "</td><td>" +
        row.create_time +
        "</td><td>" +
        row.return_time +
        "</td><td>" +
        (StrTime(new Date(), "yyyy-mm-dd") > row.return_time ? "是" : "否") +
        "</td></tr>\n";
    }

    } catch (err) {
        htm = HTM.tableBegin + HTM.tableEnd + "数据库执行失败: ";
        return HTM.head + htm + err + HTM.end;
  }
  
  return HTM.head + HTM.tableBegin + htm + HTM.tableEnd + HTM.end;
}
